package com.ruoyi.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Random;


@Component
public class MailUtils {
	//发件人邮箱
	public static String EMAIL_USER="1918503185@qq.com";
	//邮箱密码---此处为修改过后的授权码
	//uywvcpzldcarbcdh
	//qmyhhdueroarbfja  imtp
	public static String EMAIL_PWD="wpjplbdyjovsbdge";
	//发件人邮箱服务器
	public static String EMAIL_SERVER="smtp.qq.com";


	@Autowired
	public JavaMailSender mailSender;

	@Async
	public void sendMessage(List<String> emails,String context, String subject) {
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(EMAIL_USER);
			for (String email : emails) {
				helper.setTo(email);
				helper.setSubject(subject);
				helper.setText(context,true);
				mailSender.send(message);
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public void sendSimpleMail(String from, String to, String subject, String content) {
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setTo(to);
			helper.setFrom(from);
			helper.setSubject(subject);
			helper.setText(content,true);
			mailSender.send(message);
		} catch (MessagingException e) {
			throw new RuntimeException("发送失败");
		}
	}
	/**
	 * 
	 * @param session 会话
	 * @param sendMail  发送人
	 * @param receiveMail 接收人
	 * @param html  发送内容
	 * @return
	 * @throws MessagingException 
	 * @throws UnsupportedEncodingException 
	 */
	public static MimeMessage createMimeMessage(Session session,String sendMail,String receiveMail,String html) throws UnsupportedEncodingException, MessagingException{
		//1.创建邮件对象
		MimeMessage message=new MimeMessage(session);
		//2.设置发件人
		message.setFrom(new InternetAddress(sendMail));
		//3.设置收件人
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMail));
		//4.设置邮件标题
		message.setSubject("邮箱验证:","utf-8");
		//5.设置正文
		message.setContent(html,"text/html;charset=utf-8");
		//6.设置发送日期
		message.setSentDate(new Date());
		//7.保存设置
		message.saveChanges();
		return message;
	}

	//封装传输对象session
	public static Session getEmailSession(){
		
		//3.配置会话信息
		Properties pro=new Properties();
		//4.开启debug日志信息
		pro.setProperty("mail.debug", "true");
		//5.需要发送服务器身份验证
		pro.setProperty("mail.smtp.auth", "true");
		//6.设置服务器主机
		pro.setProperty("mail.host", EMAIL_SERVER);
		//7.设置遵守的协议
		pro.setProperty("mail.transport.protocol", "smtp");
		

		//创建会话
		Session session=Session.getDefaultInstance(pro,new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(EMAIL_USER,EMAIL_PWD);
            }});
		//查看session
		session.setDebug(true);
		return session;
	}


	//封装随机字符串方法:code
	public static String getCode(){
		String[] letters = new String[] {
				"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
				"A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
				"0","1","2","3","4","5","6","7","8","9"};
		StringBuffer sb=new StringBuffer();
		Random r=new Random();
		for(int i=0;i<4;i++){
			int index=r.nextInt(letters.length);
			sb.append(letters[index]);
		}
		return sb.toString();
	}

}
